<?php
include "inc/db_connect.php";
include "inc/functions.php";
session_start();
if (isset($_GET['mod']) && ($_GET['mod'] == 'excel')) {
    $filename = "VystDoch_" . $datum[0] . "_" . $datum[1] . ".xls";
    header("http-equiv='content-type' content='text/html; charset=windows-1250'");
    header("Content-type: application/vnd.ms-excel");
    header("Content-Disposition: attachment; filename=" . $filename);
    header("Expires: 0");
    header("Cache-Control: must-revalidate, post-check=0,pre-check=0");
    header("Pragma: public");
} elseif (isset($_GET['mod']) && ($_GET['mod'] == 'print')) {
    ?>
    <script language="JavaScript"  TYPE="text/Javascript">
        javascript:print('')
    </script>
    <?php
}
if (isset($_POST['zero']) && isset($_POST['user']) && isset($_POST['month'])) {
    $user_id = $_POST['user'];
    $bodNula = $_POST['zero'];
    $mesiac = $_POST['month'];

    $about_user_s = "SELECT spolid FROM user WHERE id = '" . $user_id . "' ";
    $about_user_q = MySQL_Query($about_user_s);
    $user = MySQL_Fetch_Array($about_user_q);
//echo $user['spolid'];

    if (isset($_SESSION['bmb_spolid'])) {
        $user['spolid'] = $_SESSION['bmb_spolid'];
    }

    $dni = array('ned', 'pon', 'uto', 'str', 'štv', 'pia', 'sob');
    $mesiace = array('', 'Január', 'Február', 'Marec', 'Apríl', 'Máj', 'Jún', 'Júl', 'August', 'September', 'Október', 'November', 'December');

    $dnes = date("U", mktime(0, 0, 0, date("m"), date("d"), date("Y")));
    $pocetTyzdnov = ($dnes - $bodNula) / (7 * 24 * 60 * 60);
    $pocetTyzdnov = (int) $pocetTyzdnov;
    $pocetTyzdnov++;
    $aktualnyTyzden = $pocetTyzdnov;
    $pocetTyzdnov += 10;
    if (isset($_POST['month'])) {
        $mesiac = $_POST['month'];
    } else {
        $mesiac = date("n:Y");
    }
    ?>
    <html xmlns="http://www.w3.org/1999/xhtml" xml:lang="sk" lang="sk">
        <head>

            <meta http-equiv="content-language" content="sk" />
            <meta http-equiv="cache-control" content="no-cache" />
            <meta http-equiv="content-type" content="text/html; charset=utf-8" />
            <title>BMB Leitner CS</title>
        </head>
        <body>
            <?php
            echo '<form style="margin: 10px 0; border-bottom: 1px solid gray; padding-bottom: 2px;" action="home.php?akc=77" method="post" id="kalendar">';
            echo '&nbsp;&nbsp;<span style="padding: 1px 5px; background: yellow">Sviatok</span>';
            echo '&nbsp;&nbsp;<span style="padding: 1px 5px; background: green">Manažér</span>';
            echo '&nbsp;&nbsp;<span style="padding: 1px 5px; background: #74d8d8">Dohodár</span>';
            echo '&nbsp;&nbsp;* Nočných za mesiac, Sviatkov za mesiac, Nadčas za rok = počet celých odpracovaných hodín';
            echo '</form>';

            $datum = explode(":", $mesiac);
            $pocet_dni = date("t", mktime(0, 0, 0, $datum[0], 1, $datum[1]));
            ?>
            <table style="border-collapse: collapse; font-size: 13px; text-align: center; vertical-align: middle; width: 1400px">
                <tr>
                    <td colspan="1" style="vertical-align: middle; text-align: left">
                        <img src="images/falkLogo.png" title="" alt="" width="60" height="60" />
                    </td>
                    <td colspan="34" style="vertical-align: middle; text-align: left">
                        <h1>Dochádzka za mesiac
                            <?php
                            $M = '';
                            for ($I = 0; $I < strlen($mesiac); $I++) {
                                if (($mesiac[$I]) == ':')
                                    $M .= ' / ';
                                else
                                    $M .= $mesiac[$I];
                            }
                            echo $M
                            ?>
                        </h1>
                    </td>
                </tr>
                <tr>
                    <td style="background: #cecece;border: 1px solid black;height: 20px; width: 200px;"><b><?php echo $M ?></b></td>
                    <td style="background: #cecece;border: 1px solid black;height: 20px; width: 100px;" rowspan="2"><b>Strava za <?php echo date("n/Y", mktime(0, 0, 0, $datum[0], 1, $datum[1])) ?></b></td>
                    <td style="background: #cecece;border: 1px solid black;height: 20px; width: 100px;" rowspan="2"><b>Mod za <?php echo date("Y", mktime(0, 0, 0, $datum[0], 1, $datum[1])) ?></b></td>
                    <td style="background: #cecece;border: 1px solid black;height: 20px; width: 100px;" rowspan="2"><b>Nočné za <?php echo date("n/Y", mktime(0, 0, 0, $datum[0], 1, $datum[1])) ?></b></td>
                    <td style="background: #cecece;border: 1px solid black;height: 20px; width: 100px;" rowspan="2"><b>Sviatky za <?php echo date("n/Y", mktime(0, 0, 0, $datum[0], 1, $datum[1])) ?></b></td>
                    <td style="background: #cecece;border: 1px solid black;height: 20px; width: 100px;" rowspan="2"><b>Odrobené (dohodári) za <?php echo date("n/Y", mktime(0, 0, 0, $datum[0], 1, $datum[1])) ?></b></td>
                    <?php
                    for ($i = 1; $i < $pocet_dni + 1; $i++) {
                        if (jeSviatok(date("U", mktime(0, 0, 0, $datum[0], $i, $datum[1])))) {
                            echo '<td rowspan="2" style="background: yellow;height: 20px; width: 30px; border: 1px solid black;"><b>' . $i . '</b></td>';
                        } else {
                            echo '<td rowspan="2" style="background: #eeeeee;height: 20px; width: 30px; border: 1px solid black;"><b>' . $i . '</b></td>';
                        }
                    }
                    ?>
                </tr>
                <tr>
                    <td style="background: #cecece;border: 1px solid black;height: 20px; width: 200px;"><b>Meno</b></td>
                </tr>
                <?php
                $zam_s = "SELECT * FROM z WHERE spolid='" . $user['spolid'] . "' ORDER BY priezvisko ASC";
                $zam_q = MySQL_Query($zam_s);
                while ($zam = MySQL_Fetch_Array($zam_q)) {
                    $odchod = explode('-', $zam['datum_odchodu']);
                    $prichod = explode('-', $zam['datum_prichodu']);
                    if ((((date("U", mktime(0, 0, 0, $odchod[1], $odchod[2], $odchod[0]))) >= (date("U", mktime(0, 0, 0, $datum[0], 1, $datum[1])))) || ($zam['datum_odchodu'] == '0000-00-00')) && (date("U", mktime(0, 0, 0, $prichod[1], $prichod[2], $prichod[0])) < date("U", mktime(0, 0, 0, $datum[0] + 1, 1, $datum[1])))) {
                        ?>
                        <tr>
                            <?php
                            if ($zam['manager'] == 1) {
                                ?>
                                <td style="background: green;border: 1px solid black;height: 20px; width: 200px;"><?php echo $zam['meno'] ?> <?php echo $zam['priezvisko'] ?></td>
                                <?php
                            } elseif ($zam['manager'] == 0) {
                                ?>
                                <td style="background: white;border: 1px solid black;height: 20px; width: 200px;"><?php echo $zam['meno'] ?> <?php echo $zam['priezvisko'] ?></td>
                                <?php
                            } else {
                                ?>
                                <td style="background: #74d8d8;border: 1px solid black;height: 20px; width: 200px;"><?php echo $zam['meno'] ?> <?php echo $zam['priezvisko'] ?></td>
                                <?php
                            }
                            ?>
                            <td style="background: #eee;border: 1px solid black;height: 20px; ">
                                <?php
                                $vypis = 0;
                                $nac_s = "SELECT SUM(kolko) as kolko FROM falkstrava WHERE zid = '" . $zam['id'] . "' AND mesiac='" . $datum[0] . "' AND rok='" . $datum[1] . "'";
                                $nac_q = MySQL_Query($nac_s);
                                $nac = MySQL_Fetch_Array($nac_q);
                                $vypis += $nac['kolko'];
                                echo $vypis;
                                ?>
                            </td>
                            <td style="background: #eee;border: 1px solid black;height: 20px; ">
                                <?php
                                if ($zam['manager'] != 2) {
                                    $vypis = 0;
                                    if ($zam['manager'] == 1) {
                                        $sum = vypocitajFinalnyModKMesiacu($zam['id'], $datum[0], $datum[1]);
                                        $vypis += $sum;
                                    }
                                    echo $vypis;
                                } else {
                                    echo '-';
                                }
                                ?>
                            </td>
                            <td style="background: #eee;border: 1px solid black;height: 20px; ">
                                <?php
                                if ($zam['manager'] != 2) {
                                    $vypis = 0;
                                    if ($zam['manager'] != 1) {
                                        $nac_s = "SELECT SUM(kolko) as kolko FROM falknoc WHERE zid = '" . $zam['id'] . "' AND mesiac='" . $datum[0] . "' AND rok='" . $datum[1] . "'";
                                        $nac_q = MySQL_Query($nac_s);
                                        $nac = MySQL_Fetch_Array($nac_q);
                                        $vypis += $nac['kolko'];
                                        $vypis = $vypis / 3600;
                                        $vypis = (int) $vypis;
                                    }
                                    echo $vypis;
                                } else {
                                    echo '-';
                                }
                                ?>
                            </td>
                            <td style="background: #eee;border: 1px solid black;height: 20px; ">
                                <?php
                                if ($zam['manager'] != 2) {
                                    $vypis = 0;
                                    if ($zam['manager'] != 1) {
                                        $nac_s = "SELECT SUM(kolko) as kolko FROM falksviatok WHERE zid = '" . $zam['id'] . "' AND mesiac='" . $datum[0] . "' AND rok='" . $datum[1] . "'";
                                        $nac_q = MySQL_Query($nac_s);
                                        $nac = MySQL_Fetch_Array($nac_q);
                                        $vypis += $nac['kolko'];
                                        $vypis = $vypis / 3600;
                                        $vypis = (int) $vypis;
                                    }
                                    echo $vypis;
                                } else {
                                    echo '-';
                                }
                                ?>
                            </td>
                            <td style="background: #eee;border: 1px solid black;height: 20px; ">
                                <?php
                                if ($zam['manager'] == 2) {
                                    echo pocetOdpracovanychHodin($zam['id'], $datum[0], $datum[1]);
                                } else {
                                    echo '-';
                                }
                                ?>
                            </td>

                            <?php
                            $prichodDen = date("U", mktime(0, 0, 0, $prichod[1], $prichod[2], $prichod[0]));
                            if ($prichodDen > date("U", mktime(0, 0, 0, $datum[0], 1, $datum[1]))) {
                                $pracuje = false;
                            } else {
                                $pracuje = true;
                            }
                            $odchodDen = date("U", mktime(0, 0, 0, $odchod[1], $odchod[2], $odchod[0]));
                            for ($i = 1; $i < $pocet_dni + 1; $i++) {
                                if ($prichodDen == date("U", mktime(0, 0, 0, $datum[0], $i, $datum[1]))) {
                                    $pracuje = true;
                                }
                                if ($odchodDen == date("U", mktime(0, 0, 0, $datum[0], $i - 1, $datum[1]))) {
                                    $pracuje = false;
                                }
                                if (($zam['datum_odchodu'] == '0000-00-00') || ($pracuje)) {
                                    if ((date("w", mktime(0, 0, 0, $datum[0], $i, $datum[1])) == 0) || (date("w", mktime(0, 0, 0, $datum[0], $i, $datum[1])) == 6)) {
                                        $farba = '#cecece';
                                        $border = '1px solid black';
                                    } elseif (jeSviatok(date("U", mktime(0, 0, 0, $datum[0], $i, $datum[1])))) {
                                        $farba = 'yellow';
                                        $border = '1px solid yellow';
                                    } else {
                                        $farba = 'white';
                                        $border = '1px solid black';
                                    }
                                    $nac_s = "SELECT * FROM dochadzkazamfalk WHERE date='" . (mktime(0, 0, 0, $datum[0], $i, $datum[1])) . "' AND zid='" . $zam['id'] . "' ";
                                    $nac_q = MySQL_Query($nac_s);
                                    $nac = MySQL_Fetch_Array($nac_q);
                                    if (isset($nac['id'])) {
                                        if ((($nac['type'] == 'P') && (($nac['realOd'] != '') && ($nac['realDo'] != ''))) || ($nac['type'] == 'IN')) {
                                            $hodn = 'IN';
                                            //$farba = $farby[array_search('P', $hodnoty2)];
                                        } elseif ($nac['type'] == 'DP') {
                                            $farba = $farby[array_search($nac['type'], $hodnoty2)];
                                            $hodn = 'D0.5';
                                        } elseif ($nac['type'] == 'LVP') {
                                            $farba = $farby[array_search($nac['type'], $hodnoty2)];
                                            $hodn = 'LV0.5';
                                        } elseif ($nac['type'] == 'OTP') {
                                            $farba = $farby[array_search($nac['type'], $hodnoty2)];
                                            $hodn = 'OT0.5';
                                        } elseif (($nac['type'] == 'OUT') || ((($nac['type'] == 'P') && (($nac['realOd'] == '') || ($nac['realDo'] == ''))))) {
                                            //$farba = $farby[array_search('OUT', $hodnoty2)];
                                            $hodn = '-';
                                        } else {
                                            $farba = $farby[array_search($nac['type'], $hodnoty2)];
                                            $hodn = $nac['type'];
                                        }
                                    } else {
                                        //$farba = $farby[array_search('OUT', $hodnoty2)];
                                        $hodn = '-';
                                    }
                                } else {
                                    if (jeSviatok(date("U", mktime(0, 0, 0, $datum[0], $i, $datum[1])))) {
                                        $farba = 'yellow';
                                        $border = '1px solid yellow';
                                        $hodn = '';
                                    } else {
                                        $farba = '#eee';
                                        $border = '1px solid black';
                                        $hodn = '';
                                    }
                                }
                                echo '<td rowspan="1" style="background: ' . $farba . ';height: 20px; width: 30px; border: ' . $border . ';">';
                                echo $hodn;
                                echo '</td>';
                            }
                            ?>
                        </tr>
                        <?php
                    }
                }
                ?>
            </table>
            <!-- legenda -->
            <table>
                <?php
                for ($l = 0; $l < count($hodnoty3); $l++) {
                    echo '<tr style="background: ' . $farby[$l] . '"><td>' . $hodnoty[$l] . '</td><td>' . $hodnoty3[$l] . '</td></tr>';
                }
                ?>
            </table>
        </body>
    </html>
    <!-- koniec legendy -->

    <?php
}
?>
    